package com.chen.pfm.utils;

import com.chen.pfm.entity.OrmPfmStore;
import com.chen.pfm.entity.User;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmObjectObserver;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class UserDBUtil {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x2022001, "UserDBUtil");
    static final String uriString = "dataability:///com.chen.pfm.UserDataAbility/user";



    public static boolean isExistByUid(Context context, int finduid) {
        boolean flag = false;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("uid", finduid);
        String[] columns = new String[]{"uid"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet resultSet = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (resultSet.getRowCount() > 0) {
                flag = true;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static User queryOneByUid(Context context, int finduid) {
        User user = null;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("uid", finduid);
        String[] columns = new String[]{"uid", "userid", "password"
                , "username", "gender", "birthday", "grade", "createtime"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet resultSet = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (resultSet.getRowCount() > 0) {
                // 处理结果
                resultSet.goToFirstRow();
                int uid = resultSet.getInt(0);
                String userid = resultSet.getString(1);
                String password = resultSet.getString(2);
                String username = resultSet.getString(3);
                String gender = resultSet.getString(4);
                String birthday = resultSet.getString(5);
                String grade = resultSet.getString(6);
                String createtime = resultSet.getString(7);
                user = new User(uid, userid, password, username, gender, birthday, grade, createtime);
                // 在此处理ResultSet中的记录;
                // 在此处理ResultSet中的记录;
            } else {
                return null;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return user;
    }

    public static String queryUserNameByUid(Context context, int finduid) {
        String username = null;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("uid", finduid);
        String[] columns = new String[]{"username"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet resultSet = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (resultSet.getRowCount() > 0) {
                // 处理结果
                resultSet.goToFirstRow();
                username = resultSet.getString(0);
                // 在此处理ResultSet中的记录;
                // 在此处理ResultSet中的记录;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return username;
    }

    public static int update(Context context, User user) throws DataAbilityRemoteException {
        // 构造插入数据
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造更新条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        int uid = user.getUid();
        predicates.equalTo("uid", uid);
        Uri uri = Uri.parse(uriString);
        int update = helper.update(uri, user.getValuesBucketfromObject(), predicates);
        return update;
    }

    public static int insert(Context context, User user) {
        int i = -1;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        ValuesBucket valuesBucket = user.getValuesBucketfromObject();
        try {
            i = helper.insert(uri, valuesBucket);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }

        return i;
    }

    public static int deleteByUid(Context context, int uid) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        // 构造删除条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("uid", uid);
        int i = helper.delete(uri, predicates);
        return i;
    }

    public static int delete(Context context, User user) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        // 构造删除条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("uid", user.getUid());
        int i = helper.delete(uri, predicates);
        return i;
    }

}
